<script>
import config from './config'
import store from '@/store'
import { getToken } from '@/utils/auth'

export default {
  onLaunch: function () {
    // 禁止横屏
    // #ifdef APP-PLUS
    plus.screen.lockOrientation('portrait-primary')
    // #endif
    this.initApp()
  },
  onShow: function () {
    this.updateApp()
  },
  onHide: function () {
    // #ifdef APP-PLUS
    this.timer && clearInterval(this.timer)
    // #endif
  },
  data() {
    return {
      allowSkip: false,
      needUpdate: false,
      timer: null,
      firstCheckUpdate: true
    }
  },
  methods: {
    updateApp() {
      const pages = getCurrentPages()
      if (pages.length > 0) {
        const page = pages[pages.length - 1]
        if (page.route && page.route.includes('pages/upgrade/upgrade')) {
          return
        }
      }

      // #ifdef APP-PLUS
      this.timer && clearInterval(this.timer)
      this.$store.dispatch('GetUpgradeInfo', this.allowSkip)
      this.timer = setInterval(() => {
        this.$store.dispatch('GetUpgradeInfo', this.allowSkip)
      }, 5 * 60 * 1000)
      this.allowSkip = true
      // #endif
    },
    // 初始化应用
    async initApp() {
      // 初始化应用配置
      this.initConfig()
      // 检查用户登录状态
      //#ifdef H5
      this.checkLogin()
      //#endif
      //#ifdef APP-PLUS
      this.getDeviceInfo()
      //#endif

      // 获取用户信息
      if (getToken()) {
        this.$store.dispatch('GetInfo')
        this.$store.dispatch('getImeiInfo')
      }
    },
    // 获取系统信息
    getDeviceInfo() {
      uni.getSystemInfo({
        success: (e) => {
          store.commit('SET_SYSTEM_INFO', e)
        }
      })
    },
    initConfig() {
      this.globalData.config = config
    },
    checkLogin() {
      if (!getToken()) {
        this.$tab.reLaunch('/pages/login')
      } else {
        uni.switchTab({
          url: '/pages/messages/index'
        })
      }
    }
  }
}
</script>

<style lang="scss">
@import '@/common/uni.css';
@import '@/static/scss/index.scss';
@import '@/components/active-form/css/active-form.scss';
@import '@/uni_modules/uview-ui/index.scss';
@import '@/static/iconfont/iconfont.css';
</style>
